package com.my.action;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.my.annotation.SystemOperatorLog;
import com.my.constant.Common;
import com.my.constant.ErrorCode;
import com.my.entity.Employee;
import com.my.entity.LeaveBill;
import com.my.entity.Page;
import com.my.entity.ResponseData;
import com.my.entity.vo.LeaveBillVo;
import com.my.service.IWorkflowService;
import com.my.service.LeaveBillService;

@Controller
@RequestMapping(value="/leave")
public class LeaveBillAction {
	private static final Logger logger = LoggerFactory.getLogger(LeaveBillAction.class);
	@Resource
	private LeaveBillService leaveBillService;
	@Resource
	private IWorkflowService workflowService;
	
	@RequestMapping(value="goto_leave_list")
	public String gotoLeaveList(){
		return "leave/leave_list";
	}
	/**
	 * 添加请假申请 注意：：：：使用LeavebBill接收前台的参数是，必须所有的表单的name都与LeaveBill属性对应
	 * 
	 * @return
	 */
	@RequestMapping(value = "add_leave", method = RequestMethod.POST)
	@SystemOperatorLog(description="添加请假信息")
	public @ResponseBody ResponseData<Integer> addLeaveBill(
			LeaveBill leaveBill, HttpServletRequest request) {
		Employee employee = (Employee) request.getSession().getAttribute(
				Common.CURRENT_OPERATOR.getValue());
		if (null != employee) {
			try {
				leaveBill.setUserId(employee.getId());
				leaveBillService.insertSelective(leaveBill);
				return new ResponseData<Integer>(ErrorCode.SUCCESS,
						ErrorCode.SUCCESS.getDesc(), null, false,null);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return new ResponseData<Integer>(ErrorCode.FAUILURE,
						ErrorCode.FAUILURE.getDesc(), null, true,null);
			}
		} else {
			return new ResponseData<Integer>(ErrorCode.NOT_LOGIN,
					ErrorCode.NOT_LOGIN.getDesc(), null, true,null);
		}
	}

	/**
	 * 修改
	 * 
	 * @param leaveBill
	 * @param request
	 * @return
	 */
	@RequestMapping("update_leave")
	@SystemOperatorLog(description="修改请假信息")
	public @ResponseBody ResponseData<Integer> updateLeaveBill(
			LeaveBill leaveBill, HttpServletRequest request) {
		try {
			if(leaveBill.getId()==0){
				return new ResponseData<Integer>(ErrorCode.FAUILURE,
						ErrorCode.NOT_DATA_FOUND.getDesc(), null, true,null);
			}else{
				leaveBillService.updateByPrimaryKeySelective(leaveBill);
				return new ResponseData<Integer>(ErrorCode.SUCCESS,
						ErrorCode.SUCCESS.getDesc(), null, false,null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new ResponseData<Integer>(ErrorCode.FAUILURE,
					ErrorCode.FAUILURE.getDesc(), null, true,null);
		}
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("delete_leave")
	@SystemOperatorLog(description="删除请假信息")
	public @ResponseBody ResponseData<Integer> deleteLeaveBill(Long id) {
		try {
			Assert.notNull(id);
			leaveBillService.deleteByPrimaryKey(id);
			return new ResponseData<Integer>(ErrorCode.SUCCESS,
					ErrorCode.SUCCESS.getDesc(), null, false,null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new ResponseData<Integer>(ErrorCode.FAUILURE,
					ErrorCode.FAUILURE.getDesc(), null, true,null);
		}

	}

	/**
	 * 查看所有
	 * 
	 * @return
	 */
	@RequestMapping("leave_list")
	public String selectAllLeaveBill(HttpServletRequest request, Long empId) {
		Employee employee = (Employee) request.getSession().getAttribute(
				Common.CURRENT_OPERATOR.getValue());
		List<LeaveBillVo> leaveBillList = leaveBillService
				.queryByUserId(employee.getId());
		request.setAttribute("leaveBillList", leaveBillList);
		return "leave/leave_list";
	}

	/**
	 * 查看所有（分页）
	 * 
	 * @return
	 */
	@RequestMapping("leave_list_page")
	public @ResponseBody ResponseData<LeaveBillVo> selectAllLeaveBillForPage(
			HttpServletRequest request, String empName, String leaveDate,
			Page page) {
		ResponseData<LeaveBillVo> leaveBillListForPage;
		try {
			Employee employee = (Employee) request.getSession().getAttribute(
					Common.CURRENT_OPERATOR.getValue());
			leaveBillListForPage = leaveBillService.queryByUserIdForPage(
					employee.getId(), page, leaveDate, empName);
			return leaveBillListForPage;
		} catch (Exception e) {
			e.printStackTrace();
			return new ResponseData<LeaveBillVo>(ErrorCode.FAUILURE,
					ErrorCode.FAUILURE.getDesc(), null, true,null);
		}
	}

	/**
	 * 查询的总记录数
	 * 
	 * @param request
	 * @param empName
	 * @param leaveDate
	 * @return
	 */
	@RequestMapping("leave_list_page_count")
	public @ResponseBody ResponseData<Integer> countQueryByUserIdForPage(
			HttpServletRequest request, String empName, String leaveDate) {
		try {
			int count = 0;
			Employee employee = (Employee) request.getSession().getAttribute(
					Common.CURRENT_OPERATOR.getValue());
			count = leaveBillService.countQueryByUserIdForPage(
					employee.getId(), leaveDate, empName);
			return new ResponseData<Integer>(null, count);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResponseData<Integer>(ErrorCode.FAUILURE,
					ErrorCode.FAUILURE.getDesc(), null, true,null);
		}

	}

	/**
	 * 查询单个
	 * 
	 * @return
	 */
	@RequestMapping(value = "select_leave", method = RequestMethod.POST)
	public @ResponseBody ResponseData<LeaveBill> selectLeaveBill(Long id,
			HttpServletRequest request) {
		try {
			LeaveBill leaveBill = leaveBillService.selectByPrimaryKey(id);
			return new ResponseData<LeaveBill>(ErrorCode.SUCCESS,
					ErrorCode.SUCCESS.getDesc(), Arrays.asList(leaveBill),
					false,null);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResponseData<LeaveBill>(ErrorCode.FAUILURE,
					ErrorCode.FAUILURE.getDesc(), null, true,null);
		}
	}

	/**
	 * 批量删除
	 * 
	 * @return
	 */
	@RequestMapping("batch_delete_leave")
	@SystemOperatorLog(description="批量删除请假信息")
	public @ResponseBody ResponseData<Integer> batchDeleteLeaveBill(String ids) {
		try {
			Assert.notNull(ids);
			String[] idsStr = ids.split(",");
			for (String id : idsStr) {
				leaveBillService.deleteByPrimaryKey(Long.parseLong(id));
			}
			return new ResponseData<Integer>(ErrorCode.SUCCESS,
					ErrorCode.SUCCESS.getDesc(), null, false,null);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResponseData<Integer>(ErrorCode.FAUILURE,
					ErrorCode.FAUILURE.getDesc(), null, true,null);
		}

	}

	/**
	 * 去请假管理首页
	 * 
	 * @return
	 */
	@RequestMapping(value = "leave_manager")
	public String gotoLeaveManager(HttpServletRequest request) {
		return "leave/leave_manager";
	}
}
